package com.gy.hsim.bservice.userGroupingManagement.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.support.logging.Log;
import com.gy.hsim.bservice.framework.bean.SystemConfig;
import com.gy.hsim.bservice.framework.mapper.TeamMapper;
import com.gy.hsim.bservice.userGroupingManagement.bean.Team;
import com.gy.hsim.bservice.userGroupingManagement.service.TeamService;


/**
 * @ClassName: TeamServiceImpl
 * @Description:  分组业务服务实现类
 * @author Lee
 * @date 2015-12-11 上午09:37:19
 */
@Service("TeamService")
public class TeamServiceImpl implements TeamService{
	
	@Autowired
	private TeamMapper teamMapper;
	
	@Autowired
	private SystemConfig config;
	
	@Override
	public int addTeam(Team team) throws Exception {
		int successCode = 0;
		teamMapper.addTeam(team);				
		return successCode;
	}
	
	@Override
	public int updateTeamInfo(Team team) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		teamMapper.updateTeamInfo(team);				
		return successCode;
	}

	@Override
	@Transactional
	public int deleteTeam(Team team) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		teamMapper.deleteTeam(team);
		updateTeamsetAfterDeleteTeam(team);
		return successCode;
	}

	/*@Override
	public int addFriendIntoTeam(Team team) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		String friendId = team.getFriendId();
		if(friendId !=null && friendId.length()>0){
			String [] friendIdArr = friendId.split(",");
			for(String friendIdSub : friendIdArr){
				Team teamVo = new Team();
				teamVo.setFriendId(friendIdSub);
				teamVo.setUserId(team.getUserId());
				teamVo.setTeamId(team.getTeamId());
				teamMapper.addFriendIntoTeamByUserWay(teamVo);     //通过用户方把好友添加到分组中
				teamMapper.addFriendIntoTeamByFriendWay(teamVo);   //通过好友方把好友添加到分组中
			}
		}				
		//teamMapper.addFriendIntoTeamByFriendWay(team);
		try {
			teamMapper.addFriendIntoTeamByUserWay(team);     //通过用户方把好友添加到分组中
			//teamMapper.addFriendIntoTeamByFriendWay(team);   //通过好友方把好友添加到分组中
		} catch (Exception e) {
			// TODO: handle exception
			log.debug(e.getStackTrace().toString());
			return successCode;
		}		
		return successCode;
	}*/
	
	@Override
	public int addFriendIntoTeam(List<Team> team) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;
		/*String friendId = team.getFriendId();
		if(friendId !=null && friendId.length()>0){
			String [] friendIdArr = friendId.split(",");
			for(String friendIdSub : friendIdArr){
				Team teamVo = new Team();
				teamVo.setFriendId(friendIdSub);
				teamVo.setUserId(team.getUserId());
				teamVo.setTeamId(team.getTeamId());
				teamMapper.addFriendIntoTeamByUserWay(teamVo);     //通过用户方把好友添加到分组中
				teamMapper.addFriendIntoTeamByFriendWay(teamVo);   //通过好友方把好友添加到分组中
			}
		}	*/			
		//teamMapper.addFriendIntoTeamByFriendWay(team);
		teamMapper.addFriendIntoTeamByUserWay(team);     //通过用户方把好友添加到分组中
		teamMapper.addFriendIntoTeamByFriendWay(team);   //通过好友方把好友添加到分组中
		return successCode;
	}

	@Override
	public int updateTeamsetAfterDeleteTeam(Team team) throws Exception {
		// TODO Auto-generated method stub
		int successCode = 0;		
		teamMapper.updateTeamsetAfterDeleteTeamByUserWay(team);    //删除分组后在用户方更新好友分组为空
		teamMapper.updateTeamsetAfterDeleteTeamByFriendWay(team);  //删除分组后在好友方更新好友分组为空
		return successCode;
	}

	@Override
	public List<Team> queryTeamInfoByUserId(Team team) throws Exception {
		// TODO Auto-generated method stub
		List<Team> teamList = new ArrayList<Team>();
		teamList = teamMapper.queryTeamInfoByUserId(team);
		return teamList;
	}

	@Override
	public int queryFriendSetUserBlacklisted(Team team) throws Exception {
		// TODO Auto-generated method stub
		int blacklistedCount = 0;
		int blacklistedCountByUserWay = teamMapper.queryFriendSetUserBlacklistedByUserWay(team);
		int blacklistedCountByFriendWay = teamMapper.queryFriendSetUserBlacklistedByFriendWay(team);
		blacklistedCount = blacklistedCountByUserWay + blacklistedCountByFriendWay;
		return blacklistedCount;
	}

	@Override
	public boolean getTotalTeamsUserCreateCheck(Team team) throws Exception {
		// TODO Auto-generated method stub
		boolean result = true;
		int totalTeam = teamMapper.getTotalTeamsUserCreate(team);
		int teamLimit = Integer.parseInt(config.getTeamLimit());
		if(totalTeam >= teamLimit){
			result = false;
		}
		return result;
	}

	@Override
	public boolean teamNameCheck(Team team) throws Exception {
		// TODO Auto-generated method stub
		boolean result = true;
		int teamNameCount = teamMapper.teamNameCount(team);
		if(teamNameCount >0){
			result = false;
		}
		return result;
	}

}
